package view;

import java.awt.EventQueue;

import javax.swing.JFrame;

import javax.swing.JLabel;
import java.awt.Font;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import javax.swing.SwingConstants;

import utils.ScreenAdjustment;

import java.awt.Color;

//@author A0103947A
public class PaymentAnalysis {

	private JFrame frame;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					PaymentAnalysis window = new PaymentAnalysis(500, 200);
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	
	// Attributes
	private double _initialAmount;
	private double _purchaseCost;
	private double _afterPurchaseSaved;
	private double _percentSpent;
	private String _riskLevel;
	
	/**
	 * Create the application.
	 */
	/**
	 * @param amountSaved
	 * @param costOfPurchase
	 */
	public PaymentAnalysis(double amountSaved, double costOfPurchase) {
		
		// Set attributes and perform calculations as necessary based on 
		// _initialAmount and _purchaseCost
		_initialAmount = amountSaved;
		_purchaseCost = costOfPurchase;
		
		_afterPurchaseSaved = _initialAmount-_purchaseCost;
		_percentSpent = _purchaseCost/_initialAmount * 100;
		
		if (_percentSpent <= 20)
		    _riskLevel = "affordable";
		else if (_percentSpent > 20 && _percentSpent < 50)
		    _riskLevel = "risky";
		else if (_percentSpent >= 50 && _percentSpent < 100)
		    _riskLevel= "very risky";
		else
		    _riskLevel = "impossible";
		
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	/**
	 * 
	 * @Precondition 
	 * @Postcondition 
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(ScreenAdjustment.getAlignCenterX(450), ScreenAdjustment.getAlignCenterY(300) , 450, 300);
		frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		frame.setVisible(true);
		frame.getContentPane().setLayout(null);
		
		
		JLabel lblTitle = new JLabel("Payment Analysis");
		lblTitle.setHorizontalAlignment(SwingConstants.CENTER);
		lblTitle.setBounds(119, 11, 206, 25);
		lblTitle.setFont(new Font("Century Gothic", Font.PLAIN, 20));
		frame.getContentPane().add(lblTitle);
		
		JLabel lblAmountSaved = new JLabel("Current Balance: ");
		lblAmountSaved.setHorizontalAlignment(SwingConstants.RIGHT);
		lblAmountSaved.setBounds(109, 60, 137, 16);
		frame.getContentPane().add(lblAmountSaved);
		
		JLabel labelSavedValue = new JLabel("$" + returnTwoDecimals(_initialAmount));
		labelSavedValue.setBounds(251, 60, 81, 16);
		frame.getContentPane().add(labelSavedValue);
		
		JLabel lblTargetPurchaseAmount = new JLabel("Target Purchase Amount: ");
		lblTargetPurchaseAmount.setHorizontalAlignment(SwingConstants.TRAILING);
		lblTargetPurchaseAmount.setBounds(84, 81, 162, 16);
		frame.getContentPane().add(lblTargetPurchaseAmount);
		
		JLabel lblPurchaseCost = new JLabel("$" + returnTwoDecimals(_purchaseCost));
		lblPurchaseCost.setBounds(251, 81, 81, 16);
		frame.getContentPane().add(lblPurchaseCost);
		
		JLabel lblSavingsAfterPurchase = new JLabel("Balance after purchase would be $" + returnTwoDecimals(_afterPurchaseSaved) + ".");
		lblSavingsAfterPurchase.setBounds(117, 140, 292, 16);
		frame.getContentPane().add(lblSavingsAfterPurchase);
		
		JLabel lblPercentSavingsSpent = new JLabel("Purchase would cost " + returnTwoDecimals(_percentSpent) + "% of savings.");
		lblPercentSavingsSpent.setBounds(117, 168, 288, 16);
		frame.getContentPane().add(lblPercentSavingsSpent);
		
		JLabel lblRiskLevelString = new JLabel("EzXpns classifies purchase as "/* '" + _riskLevel + "'."*/);
		lblRiskLevelString.setBounds(117, 196, 292, 16);
		frame.getContentPane().add(lblRiskLevelString);
		
		JLabel lbl_risklevel = new JLabel("");
		lbl_risklevel.setFont(new Font("Century Gothic", Font.BOLD | Font.ITALIC, 16));
		lbl_risklevel.setBounds(271, 190, 88, 31);
		lbl_risklevel.setText(_riskLevel);

		if (_percentSpent <= 20)
			lbl_risklevel.setForeground(new Color(51, 153, 0));
		else if (_percentSpent > 20 && _percentSpent < 50)
			lbl_risklevel.setForeground(new Color(255, 140, 0));
		else if (_percentSpent >= 50 && _percentSpent < 100)
			lbl_risklevel.setForeground(new Color(255, 0, 0));
		else
			lbl_risklevel.setForeground(new Color(153, 0, 255));
		frame.getContentPane().add(lbl_risklevel);
	}
	

	// Function that changes a double value to String with exactly two decimals
	/**
	 * 
	 * @param value
	 * @return
	 * @Precondition 
	 * @Postcondition 
	 */
	private String returnTwoDecimals(double value) {
		NumberFormat df = DecimalFormat.getInstance();
        df.setMinimumFractionDigits(2);
        df.setMaximumFractionDigits(2);
        return (df.format(value));
	}

}